package com.example.controlleradvice.system;

import com.example.controlleradvice.global.GlobalControllerAdvice;
import com.example.response.ErrorStatus;
import com.example.response.ApiResult;
import com.example.util.RabbitMQUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.Date;

/**
 * 用于捕获未在 {@link GlobalControllerAdvice} 且未在其他控制器中捕获的异常
 * 一般视为服务器异常处理
 * @author LeXin Huang
 * @date 2021年04月09日 11:51
 */
@Slf4j
@Order
@RestControllerAdvice
public class SystemErrorControllerAdvice {

    @ExceptionHandler(value = Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ApiResult<ApiResult.ApiData> handleAll(Exception e) {
        RabbitMQUtil.publishServerErrorInfo("------------------------异常信息------------------------");
        RabbitMQUtil.publishServerErrorInfo(e.getMessage());
        return ApiResult.buildErrorResponse(ErrorStatus.SERVER_ERROR);
    }

}
